package com.iafme.freemarker;



import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;

import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import com.aspose.words.*;


import com.iafme.dto.res.MemberOrderResDTO;


import com.iafme.dto.res.PdfResDTO;
import com.iafme.model.Rules;
import com.iafme.service.IOrderService;
import com.iafme.service.RulesService;
import com.systemframework.exception.ResultException;
import com.systemframework.oss.OssConfigure;
import com.systemframework.properties.SaTokenProperties;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2025/3/27 19:44
 */
@Component
@AllArgsConstructor
@Slf4j
public class TemplateConfiguration {


    private final SaTokenProperties saTokenProperties;

    private final IOrderService orderService;

    private final OssConfigure ossConfigure;

    private final RulesService rulesService;


    public PdfResDTO sendPdF(Integer orderId)  {
        try {
            PdfResDTO pdfResDTO = new PdfResDTO();
            MemberOrderResDTO memberOrder = orderService.findMemberOrder(orderId);
            String outFile=saTokenProperties.getFilePath()+"template.docx";
            String pdfFile=saTokenProperties.getFilePath()+"template.pdf";
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
            configuration.setDefaultEncoding(StandardCharsets.UTF_8.name());
            configuration.setClassForTemplateLoading(TemplateConfiguration.class, "/template/");
            Template template = configuration.getTemplate("template.ftl");
            Map<String, Object> dataModel = MapUtil.newHashMap();
            dataModel.put("order",memberOrder);
            dataModel.put("courserList",memberOrder.getCourseResDTOList());
            List<Rules> list = rulesService.list();
            dataModel.put("ruleList",list);
            Writer out = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(FileUtil.file(outFile).toPath()), Charset.defaultCharset()));
            template.process(dataModel, out);
            Document document=new Document(outFile);
            document.save(pdfFile, SaveFormat.PDF);
            String path = ossConfigure.uploadFile(FileUtil.file(pdfFile));
            pdfResDTO.setPath(path);
            FileUtil.del(outFile);
            FileUtil.del(pdfFile);
            return pdfResDTO;
        }catch (Exception e){
            log.error("error:{},{}",e,e.getMessage());
            throw new ResultException("生成PDF出错");
        }
    }



}
